package com.yuerlive.rainbow.common;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.controller.LiveRoomController;
import com.yuerlive.rainbow.entity.Banner;
import com.yuerlive.rainbow.module.homepage.HomePageActivity;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.module.web.WebViewActivity;
import com.yuerlive.rainbow.utils.GlideUtil;

import java.util.List;

/**
 * Created by arvin on 2015/11/15.
 */
public class ImagePagerAdapter<T> extends RecyclingPagerAdapter {

    private Context context;
    private List<T> list;

    public ImagePagerAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup container) {
        ViewHolder holder;
        final Banner banner = (Banner) list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.headline_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.img_headlineitem_banner);
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;


                switch (banner.getType()) {
                    //TODO 跳转测试
                    case 1: //纯图片不跳转

                        break;
                    case 2: //直播
                        if(banner.getState()!=0) {
                            intent = new Intent(context, LiveRoomActivity.class);
                            intent.putExtra("liveId", banner.getLiveId());
                            intent.putExtra("chatroomId", banner.getChatRoomId());
                            intent.putExtra("liveCover", banner.getLiveIcon());
                            LiveRoomController.getInstance().enterLiveRoom(context,intent);
                        }else{
                            intent = new Intent(context, HomePageActivity.class);
                            intent.putExtra("otherId",banner.getUserId());
                            context.startActivity(intent);
                        }
                        break;
                    case 3: //webview 页面
                        intent = new Intent(context, WebViewActivity.class);
                        intent.putExtra("url", banner.getTarget() + "");
                        context.startActivity(intent);
                        break;
                }

            }
        });
        GlideUtil.load(context, banner.getImg(), holder.imageView);
        //图片展示逻辑
        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView;
        TextView lable;
    }
}
